/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatroom.com.frame;

import chatroom.com.chatEvent.Chat;
import chatroom.com.clientserver.ChatServer;
import chatroom.com.clientserver.ServerThread;
import chatroom.com.entity.MessageModel;
import chatroom.com.entity.UserModel;
import java.awt.Color;
import java.util.Vector;
import javax.swing.*;

/**
 *
 * @author Administrator
 */
public class AdminFrame extends javax.swing.JFrame {

    private static DefaultListModel listModel;

    /**
     * Creates new form AdminFrame
     */
    public AdminFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        p1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        xiaoxi = new javax.swing.JTextPane();
        jPanel1 = new javax.swing.JPanel();
        userCount = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listModel = new DefaultListModel();
        userList = new javax.swing.JList(listModel);
        jPanel2 = new javax.swing.JPanel();
        qidong = new javax.swing.JButton();
        guanbi = new javax.swing.JButton();
        tirenduixiang = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        tiren = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        message = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        gg = new javax.swing.JTextField();
        guanggao = new javax.swing.JButton();
        fasonggonggao = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("管理员窗口");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        xiaoxi.setEditable(false);
        xiaoxi.setDoubleBuffered(true);
        xiaoxi.setDragEnabled(true);
        jScrollPane1.setViewportView(xiaoxi);

        javax.swing.GroupLayout p1Layout = new javax.swing.GroupLayout(p1);
        p1.setLayout(p1Layout);
        p1Layout.setHorizontalGroup(
            p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
        );
        p1Layout.setVerticalGroup(
            p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        userCount.setForeground(new java.awt.Color(255, 0, 0));
        userCount.setText("在线人数：");

        jScrollPane2.setViewportView(userList);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(userCount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(userCount, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("功能区域"));

        qidong.setText("重启服务器");
        qidong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                qidongActionPerformed(evt);
            }
        });

        guanbi.setText("关闭服务器");
        guanbi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guanbiActionPerformed(evt);
            }
        });

        tirenduixiang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "请选择" }));

        jLabel2.setText("踢人");

        tiren.setText("确定踢人");
        tiren.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tirenActionPerformed(evt);
            }
        });

        jLabel3.setText("发送系统公告：");

        message.setColumns(20);
        message.setRows(5);
        jScrollPane3.setViewportView(message);

        jLabel4.setText("设置广告");

        gg.setText(" ");

        guanggao.setText("确定");
        guanggao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guanggaoActionPerformed(evt);
            }
        });

        fasonggonggao.setText("发送");
        fasonggonggao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fasonggonggaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(guanggao)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(fasonggonggao))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(gg))
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(guanbi, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(qidong, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tiren, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(tirenduixiang, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(fasonggonggao))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(tirenduixiang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(tiren)
                        .addGap(28, 28, 28)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(gg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(qidong))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(guanggao)
                    .addComponent(guanbi)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(p1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(p1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void qidongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_qidongActionPerformed
        if (evt.getSource() == qidong) {
            ChatServer.stopServer();
            ChatServer.startServer();
        }
    }//GEN-LAST:event_qidongActionPerformed

    private void guanbiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guanbiActionPerformed
        ChatServer.stopServer();
    }//GEN-LAST:event_guanbiActionPerformed

    /**
     * 踢人事件：踢人时候
     *
     * @param evt
     */
    private void tirenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tirenActionPerformed
        if (tirenduixiang.getSelectedItem().toString().equals("请选择")) {
            JOptionPane.showMessageDialog(null, "请先选择一个用户，才能进行踢人！");
        } else {
            ChatServer.sendMsgToAll("quit&" + tirenduixiang.getSelectedItem().toString() + "&" + " 退出了聊天室" + "&" + ChatServer.getClients().size());
            ChatServer.deleteConnectionByName(tirenduixiang.getSelectedItem().toString());
        }
    }//GEN-LAST:event_tirenActionPerformed

    private void guanggaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guanggaoActionPerformed
        if (gg.getText().length() == 0) {
            JOptionPane.showMessageDialog(null, "发送的广告不能为空！");
            return;
        }
        ChatServer.sendMsgToAll("guanggao&" + gg.getText());
        gg.setText("");
    }//GEN-LAST:event_guanggaoActionPerformed

    private void fasonggonggaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fasonggonggaoActionPerformed
        if (message.getText().length() == 0) {
            JOptionPane.showMessageDialog(null, "发送的系统公告不能为空！");
            return;
        }
        ChatServer.sendMsgToAll("gonggao&" + message.getText());
        message.setText("");
    }//GEN-LAST:event_fasonggonggaoActionPerformed

    /**
     * 功能：新加入一个聊天用户
     *
     * @param user
     */
    public static void addNewUser(UserModel user, int count) {
        String userStr = UserModel.userToString(user);
        String[] temp = userStr.split("&");
        userCount.setText("在线人数：" + count);

        listModel.addElement(temp[2] + " 〖" + temp[3] + "〗");
        tirenduixiang.addItem(temp[2]);
        Chat.sysXiaoxi(xiaoxi, user.getNick() + "加入了聊天室", Color.BLUE, 20);
    }

    /**
     * 功能：退出一个用户
     *
     * @param index
     * @param userNick
     */
    public static void deleteOneUser(int index, String userNick) {
        listModel.removeElementAt(index - 1);
        tirenduixiang.removeItem(userNick);
        userCount.setText("在线人数：" + (ChatServer.getClients().size() - 1));
        Chat.sysXiaoxi(xiaoxi, userNick + "离开了聊天室", Color.BLUE, 20);
    }

    /**
     * 将服务器收到的消息插入到面板
     *
     * @param str
     */
    public static void insertXiaoxi(String str) {
        Chat.sysXiaoxi(xiaoxi, str, Color.black, 12);
    }

    public static void insertHuaToPane(MessageModel msg) {
        String str = msg.getFromWho() + "  送给  " + msg.getToWho() + "  朵  ";
        Chat.inserts(xiaoxi, str, msg, message);
    }

    /**
     * 功能：将聊天信息插入到服务器接收面板
     *
     * @param msg
     */
    public static void insertMessageToJTextPane(MessageModel msg) {
        String str = msg.getFromWho() + " 对 " + msg.getToWho() + " 说:";
        if (!msg.getDongzuo().equals("无")) {
            str = msg.getFromWho() + " 对 " + msg.getToWho() + "  " + msg.getDongzuo() + " 说:";
        }
        Chat.inserts(xiaoxi, str, msg, message);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AdminFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AdminFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AdminFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AdminFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                AdminFrame af = new AdminFrame();
                af.setLocation(220, 20);
                af.setVisible(true);
            }
        });
        ChatServer.startServer();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton fasonggonggao;
    private javax.swing.JTextField gg;
    private javax.swing.JButton guanbi;
    private javax.swing.JButton guanggao;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private static javax.swing.JTextArea message;
    private javax.swing.JPanel p1;
    private javax.swing.JButton qidong;
    private javax.swing.JButton tiren;
    private static javax.swing.JComboBox tirenduixiang;
    private static javax.swing.JLabel userCount;
    private static javax.swing.JList userList;
    private static javax.swing.JTextPane xiaoxi;
    // End of variables declaration//GEN-END:variables

    public JButton getFasonggonggao() {
        return fasonggonggao;
    }

    public void setFasonggonggao(JButton fasonggonggao) {
        this.fasonggonggao = fasonggonggao;
    }

    public JButton getGuanbi() {
        return guanbi;
    }

    public void setGuanbi(JButton guanbi) {
        this.guanbi = guanbi;
    }

    public JButton getjButton1() {
        return guanggao;
    }

    public void setjButton1(JButton jButton1) {
        this.guanggao = jButton1;
    }

    public JComboBox getjComboBox1() {
        return tirenduixiang;
    }

    public void setjComboBox1(JComboBox jComboBox1) {
        this.tirenduixiang = jComboBox1;
    }

    public JLabel getUserCount() {
        return userCount;
    }

    public void setUserCount(JLabel userCount) {
        this.userCount = userCount;
    }

    public JLabel getjLabel2() {
        return jLabel2;
    }

    public void setjLabel2(JLabel jLabel2) {
        this.jLabel2 = jLabel2;
    }

    public JLabel getjLabel3() {
        return jLabel3;
    }

    public void setjLabel3(JLabel jLabel3) {
        this.jLabel3 = jLabel3;
    }

    public JLabel getjLabel4() {
        return jLabel4;
    }

    public void setjLabel4(JLabel jLabel4) {
        this.jLabel4 = jLabel4;
    }

    public JList getjList1() {
        return userList;
    }

    public void setjList1(JList jList1) {
        this.userList = jList1;
    }

    public JPanel getjPanel1() {
        return jPanel1;
    }

    public void setjPanel1(JPanel jPanel1) {
        this.jPanel1 = jPanel1;
    }

    public JPanel getjPanel2() {
        return jPanel2;
    }

    public void setjPanel2(JPanel jPanel2) {
        this.jPanel2 = jPanel2;
    }

    public JScrollPane getjScrollPane1() {
        return jScrollPane1;
    }

    public void setjScrollPane1(JScrollPane jScrollPane1) {
        this.jScrollPane1 = jScrollPane1;
    }

    public JScrollPane getjScrollPane2() {
        return jScrollPane2;
    }

    public void setjScrollPane2(JScrollPane jScrollPane2) {
        this.jScrollPane2 = jScrollPane2;
    }

    public JScrollPane getjScrollPane3() {
        return jScrollPane3;
    }

    public void setjScrollPane3(JScrollPane jScrollPane3) {
        this.jScrollPane3 = jScrollPane3;
    }

    public JTextArea getjTextArea1() {
        return message;
    }

    public void setjTextArea1(JTextArea jTextArea1) {
        this.message = jTextArea1;
    }

    public JTextField getjTextField1() {
        return gg;
    }

    public void setjTextField1(JTextField jTextField1) {
        this.gg = jTextField1;
    }

    public JTextPane getJtp1() {
        return xiaoxi;
    }

    public void setJtp1(JTextPane jtp1) {
        this.xiaoxi = jtp1;
    }

    public JPanel getP1() {
        return p1;
    }

    public void setP1(JPanel p1) {
        this.p1 = p1;
    }

    public JButton getQidong() {
        return qidong;
    }

    public void setQidong(JButton qidong) {
        this.qidong = qidong;
    }

    public JButton getTiren() {
        return tiren;
    }

    public void setTiren(JButton tiren) {
        this.tiren = tiren;
    }

    public DefaultListModel getListModel() {
        return listModel;
    }

    public void setListModel(DefaultListModel listModel) {
        AdminFrame.listModel = listModel;
    }

    public JComboBox getTirenduixiang() {
        return tirenduixiang;
    }

    public void setTirenduixiang(JComboBox tirenduixiang) {
        AdminFrame.tirenduixiang = tirenduixiang;
    }
}
